package com.ton.system.service;

import com.ton.config.NetContext;
import com.hai.admin.table.service.IBeanService;
import com.ton.utils.AccessUtil;
import com.ton.utils.CacheUtil;
import com.kypj.frame.common.EmptyUtil;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class BeanService implements IBeanService {
    @Override
    public <T> T getInstance(String name) {
        return NetContext.getInstance(name);
    }

    @Override
    public <T> T getInstance(Class clazz) {
        return NetContext.getInstance(clazz);
    }


    @Override
    public boolean checkPermission(String permissions, String requestSource) {
        Integer userId = AccessUtil.getAdminLoginUserId();
        if (EmptyUtil.isEmpty(permissions)) {
            return true;
        }
        List<String> permissionList = CacheUtil.queryObject("admin_permission_" + userId);
        if (permissionList.contains("super_permission")) {
            return true;
        }
        String[] ps = permissions.split(",");
        for (String p : ps) {
            if (EmptyUtil.isNotEmpty(p) && permissionList.contains(p)) {
                return true;
            }
        }
        return false;
    }

    @Override
    public Integer getAdminId(String requestSource) {
        return AccessUtil.getAdminLoginUserId();
    }
}
